package setting

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/setting"
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var ChannelGroup = core.Group("/channel", newChannelHandler, regChannel, middleware.TokenAuth())

func newChannelHandler(srv setting.ISettingChannelService) *channelHandler {
	return &channelHandler{srv: srv}
}

func regChannel(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *channelHandler) {
		rg.GET("/mp/detail", handle.mpDetail)
		rg.POST("/mp/save", handle.mpEdit)
		rg.GET("/open/detail", handle.openDetail)
		rg.POST("/open/save", handle.openEdit)
		rg.GET("/oa/detail", handle.oaDetail)
		rg.POST("/oa/save", handle.oaEdit)
		rg.GET("/oa/menu", handle.oaMenuDetail)
		rg.POST("/oa/menu", handle.oaMenuEdit)
		rg.GET("/h5/detail", handle.h5Detail)
		rg.POST("/h5/save", handle.h5Edit)
		rg.GET("/oa/reply/lists", handle.oaReplyList)
		rg.GET("/oa/reply/detail", handle.oaReplyDetail)
		rg.POST("/oa/reply/add", handle.oaReplyAdd)
		rg.POST("/oa/reply/edit", handle.oaReplyEdit)
		rg.POST("/oa/reply/status", handle.oaReplyChange)
		rg.POST("/oa/reply/delete", handle.oaReplyDelete)
	})
}

type channelHandler struct {
	srv setting.ISettingChannelService
}

// mpDetail 小程序配置详情
func (sh channelHandler) mpDetail(c *gin.Context) {
	res, err := sh.srv.MpDetail(c)
	response.CheckAndRespWithData(c, res, err)
}

// mpEdit 小程序保存
func (sh channelHandler) mpEdit(c *gin.Context) {
	var editReq req.ChannelMnpSettingReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, sh.srv.MpEdit(editReq))
}

// openDetail 开放平台详情
func (sh channelHandler) openDetail(c *gin.Context) {
	res, err := sh.srv.OpenDetail()
	response.CheckAndRespWithData(c, res, err)
}

// openEdit 开放平台保存
func (sh channelHandler) openEdit(c *gin.Context) {
	var editReq req.ChannelOpenSettingReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, sh.srv.OpenEdit(editReq))
}

// oaDetail 开放平台详情
func (sh channelHandler) oaDetail(c *gin.Context) {
	res, err := sh.srv.OADetail(c)
	response.CheckAndRespWithData(c, res, err)
}

// oaEdit 开放平台保存
func (sh channelHandler) oaEdit(c *gin.Context) {
	var editReq req.ChannelOASettingReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, sh.srv.OAEdit(editReq))
}

// h5Detail 开放平台详情
func (sh channelHandler) h5Detail(c *gin.Context) {
	res, err := sh.srv.H5Detail(c)
	response.CheckAndRespWithData(c, res, err)
}

// h5Edit 开放平台保存
func (sh channelHandler) h5Edit(c *gin.Context) {
	var editReq req.ChannelH5SettingReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, sh.srv.H5Edit(editReq))
}

// oaMenuDetail 公众号菜单详情
func (sh channelHandler) oaMenuDetail(c *gin.Context) {
	res, err := sh.srv.OaMenuDetail()
	response.CheckAndRespWithData(c, res, err)
}

// oaMenuEdit 菜单保存保存
func (sh channelHandler) oaMenuEdit(c *gin.Context) {
	var editReq []req.ChannelOaMenuReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, sh.srv.OaMenuEdit(editReq))
}

// oaReplyAdd 添加回复
func (sh channelHandler) oaReplyAdd(c *gin.Context) {
	var addReq req.OfficialReplyAddReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &addReq)) {
		return
	}
	response.CheckAndResp(c, sh.srv.OaReplyAdd(c, addReq))
}

// oaReplyEdit 编辑回复
func (sh channelHandler) oaReplyEdit(c *gin.Context) {
	var editReq req.OfficialReplyEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, sh.srv.OaReplyEdit(c, editReq))
}

// oaReplyChange 切换状态
func (sh channelHandler) oaReplyChange(c *gin.Context) {
	var req req.OfficialReplyDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &req)) {
		return
	}
	response.CheckAndResp(c, sh.srv.OaReplyStatus(c, req.Id))
}

// oaReplyDelete 删除
func (sh channelHandler) oaReplyDelete(c *gin.Context) {
	var req req.OfficialReplyDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyJSON(c, &req)) {
		return
	}
	response.CheckAndResp(c, sh.srv.OaReplyDel(c, req.Id))
}

// oaReplyDetail 详情
func (sh channelHandler) oaReplyDetail(c *gin.Context) {
	var detailReq req.OfficialReplyDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := sh.srv.OaReplyDetail(c, detailReq.Id)
	response.CheckAndRespWithData(c, res, err)
}

// list 列表
func (sh channelHandler) oaReplyList(c *gin.Context) {
	var page request.PageReq
	var listReq req.OfficialReplyListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := sh.srv.OaReplyList(c, page, listReq)
	response.CheckAndRespWithData(c, res, err)
}
